<?php
global $_W, $_GPC;
$uid = $_GPC['uid'];
if(empty($uid)){
    return $this->getresult(1,'请先登录',array('code'=>3001,'msg'=>'请先登录'));
}
/***校验token开始***/
if($uid){
    $token = $_GPC['token'];
    if (empty($token)) {
        return $this->getresult(1, '请重新登录', ['code' => 4001, 'msg' => '请重新登录']);
    }
    $token_result = $this->checktoken($uid, $token);
    if ($token_result == 1002) {
        return $this->getresult(1, '请重新登录', ['code' => 4002, 'msg' => '请重新登录']);
    } elseif ($token_result == 1003) {
        return $this->getresult(1, '请重新登录', ['code' => 4003, 'msg' => '请重新登录']);
    }elseif ($token_result == 1001) {
        //  更新token
        pdo_update($this->t_user_token,array('createtime'=>time()),array('uid' => $uid, 'token' => $token, 'weid' => $_W['uniacid']));
    }
}
/***校验token结束***/
$weid = $_W['uniacid'];
//获取线上已报名的课程
$contion = 's.weid = :weid AND s.uid = :uid';
$params = [':weid' => $weid, ':uid' => $uid];
$lessonList = [];
$videoLesson = pdo_fetchall("select count(p.id) as pnum, s.id,s.uid,sj.name as subjectname,l.title as ltitle,l.id as lid,l.is_underline from " . tablename('goouc_class_user_signup') . " s LEFT JOIN " . tablename('goouc_class_lesson') . " l ON s.lessonid = l.id LEFT JOIN " . tablename('goouc_class_subject') . " sj ON l.subjectid = sj.id LEFT JOIN " . tablename('goouc_class_practice') . "p ON p.lessonid = l.id where " . $contion . " group by l.id", $params);
//把存在习题的课程放在一个数组里
$practiceLesson = [];
foreach ($videoLesson as $kv => $vv) {
    if ($vv['pnum'] > 0) {
        $practiceLesson[] = $vv;
    }
}
//取线下课程
$phone = pdo_fetchcolumn("select phone from " . tablename('goouc_class_user') . "  where id = :uid", array(':uid' => $uid));
$uparams = [':weid' => $weid, ':phone' => $phone, ':is_underline' => 1];
$underlineList = pdo_fetchall("select count(p.id) as pnum,sj.name as subjectname,l.title as ltitle,l.id as lid ,l.is_underline from " . tablename('goouc_class_lesson_underline_student') . " us LEFT JOIN " . tablename('goouc_class_lesson') . " l ON us.lesson_id = l.id LEFT JOIN " . tablename('goouc_class_subject') . " sj ON l.subjectid = sj.id LEFT JOIN " . tablename('goouc_class_practice') . "p ON p.lessonid = l.id  where us.weid = :weid AND us.phone = :phone AND l.is_underline = :is_underline group by l.id", $uparams);
foreach ($underlineList as $ks => $vs) {
    if ($vs['pnum'] > 0) {
        $practiceLesson[] = $vs;
    }
}
foreach ($practiceLesson as &$v) {
    //章节总数量
    $tsql = "SELECT COUNT(*) FROM " . tablename('goouc_class_son') . " WHERE weid = :weid AND lessonid = :lessonid AND status = :status";
    $chaptertotal = pdo_fetchcolumn($tsql, array(':weid' => $weid, ':lessonid' => $v['lid'], ':status' => 1));
    //课程总星星数量
    $v['totalStar'] = 3 * $chaptertotal;
    //会员该课程获取到的星星数量
    $userstar = pdo_fetch("select sum(num) as totalnum from " . tablename('goouc_class_user_star') . " WHERE weid = :weid AND lessonid = :lessonid AND uid = :uid AND status = :status", array(':weid' => $weid, ':uid' => $uid, ':lessonid' => $v['lid'], ':status' => 1));
    $v['user_star'] = empty($userstar['totalnum']) ? 0 : $userstar['totalnum'];
    //学习进度条
    $studySon = pdo_fetchcolumn("select COUNT(*) as studySon FROM " . tablename('goouc_class_stady') . " cs LEFT JOIN " . tablename('goouc_class_son') . " s ON cs.sonid = s.id LEFT JOIN " . tablename('goouc_class_lesson') . " l ON s.lessonid = l.id where cs.weid = :weid AND cs.uid = :uid AND l.id = :lid AND s.status = :status", array(':weid' => $weid, ':uid' => $uid, ':lid' => $v['lid'], ':status' => 1));
    $v['jindu'] = $studySon / $chaptertotal;
    //最近一场直播时间
    //$nearvideo = pdo_fetch("select starttime from " . tablename('goouc_class_son') . " WHERE weid = :weid AND lessonid = :lessonid AND status = :status AND sontype <> :sontype AND starttime > :starttime AND starttime <= :endtime ORDER BY starttime asc limit 1", array(':weid' => $weid, ':lessonid' => $v['lid'], ':sontype' => 2, ':status' => 1, ':starttime' => strtotime(date('Y-m-d 00:00:00', time())), ':endtime' => strtotime(date('Y-m-d 23:59:59', time()))));
    //$v['video'] = empty($nearvideo['starttime']) ? 0 : date('H:s', $nearvideo['starttime']);//当天的直播章节时间
    //该课程学习状态
    $studyStatus = pdo_fetch("select id FROM " . tablename('goouc_class_user_study_speed') . " WHERE weid = :weid AND lessonid = :lessonid AND uid = :uid limit 1", array(':weid' => $weid, ':lessonid' => $v['lid'], ':uid' => $uid));
    $v['studyStatus'] = empty($studyStatus['id']) ? 2 : 1;//1:已学习 2:未学习
}
return $this->getresult(0, '课程错题列表', ['code' => 200, 'listdata' => $practiceLesson]);